package com.aboverock.module.system.starter;

import com.aboverock.autoconfigure.properties.ShiroProperties;
import com.aboverock.autoconfigure.properties.SuperAdminProperties;
import com.aboverock.module.shiro.util.ShiroCacheUtil;
import com.aboverock.module.system.domain.SuperAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({ SuperAdminProperties.class, ShiroProperties.class})
class AdminAutoConfiguration {

    @Autowired
    private SuperAdminProperties superAdminProperties;

    @Autowired
    private ShiroProperties shiroProperties;

    @Bean
    @ConditionalOnMissingBean(SuperAdmin.class)
    public SuperAdmin superAdmin() {
        return new SuperAdmin(superAdminProperties.getName(), superAdminProperties.getPassword());
    }

    @Bean
    @ConditionalOnMissingBean(ShiroCacheUtil.class)
    public ShiroCacheUtil shiroCacheUtil() {
        return new ShiroCacheUtil(shiroProperties.getCacheAuthentication(), shiroProperties.getCacheAuthorization());
    }
}

